﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TuongLaiDangCho.DTO;
using TuongLaiDangCho.DAO;

namespace TuongLaiDangCho.Enterface
{
    public partial class ucWriteDiary : DevExpress.XtraEditors.XtraUserControl
    {
        string baseContain;
        bool flag_edit;
        NhatKiDTO diary;
        public ucWriteDiary()
        {
            InitializeComponent();
            flag_edit = false;
            diary = new NhatKiDTO();
            diary.Ngay = frmHome.choiseDate;
            groWriteDiary.Text = DataProvider.Today(diary.Ngay)+" "+frmHome.choiseDate.ToShortDateString();
            baseContain = "";
        }
        private void updateStatus()
        {
            if (memoEdit.Text != baseContain)
            {
                groWriteDiary.Text = DataProvider.Today(diary.Ngay) + " " + frmHome.choiseDate.ToShortDateString() + "*";
                //groWriteDiary.Text = "";
            }
            else
            {
                groWriteDiary.Text = DataProvider.Today(diary.Ngay) + " " + frmHome.choiseDate.ToShortDateString();
            }
        }
        private void bntClose_Click(object sender, EventArgs e)
        {
            if (memoEdit.Text == baseContain)
            {                
                frmHome.changeUserControl();
            }
            else
            {
                DialogResult dr_MessageResult;
                dr_MessageResult=XtraMessageBox.Show("Do you want save change?", "Diary", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk);
                switch (dr_MessageResult)
                {
                    case DialogResult.Yes:
                        if (flag_edit == true)
                        {
                            diary.Noidung = memoEdit.Text;
                            NhatKiDAO.UpdateDiary(diary);
                        }
                        else
                        {
                            diary.Noidung = memoEdit.Text;
                            NhatKiDAO.InsertDiary(diary);
                        }
                        frmHome.changeUserControl();
                        break;
                    case DialogResult.No:
                        frmHome.changeUserControl();
                        break;
                    case DialogResult.Cancel:
                        return;
                        //break;
                    default:
                        break;
                }


            }
        }

        private void memoEdit_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
        {
            //MessageBox.Show(memoEdit.Text);
            updateStatus();
        }

        private void ucWriteDiary_Load(object sender, EventArgs e)
        {
            NhatKiDTO diary = new NhatKiDTO();
            diary.Ngay = frmHome.choiseDate;
            if (NhatKiDAO.IsDate(diary))
            {
                memoEdit.Text = NhatKiDAO.LoadDiary(diary);
                baseContain = memoEdit.Text;
                flag_edit = true;
            }
        }

        private void bntSave_Click(object sender, EventArgs e)
        {
            if (flag_edit)
            {
                diary.Noidung = memoEdit.Text;
                NhatKiDAO.UpdateDiary(diary);
                baseContain = memoEdit.Text;
                updateStatus();
            }
            else
            {
                diary.Noidung = memoEdit.Text;
                NhatKiDAO.InsertDiary(diary);
                baseContain = memoEdit.Text;
                updateStatus();
            }
        }

        private void bntSaveAndClose_Click(object sender, EventArgs e)
        {
            if (flag_edit)
            {
                diary.Noidung = memoEdit.Text;
                NhatKiDAO.UpdateDiary(diary);
                baseContain = memoEdit.Text;
                frmHome.changeUserControl();
            }
            else
            {
                diary.Noidung = memoEdit.Text;
                NhatKiDAO.InsertDiary(diary);
                baseContain = memoEdit.Text;
                frmHome.changeUserControl();
            }
        }

    }
}
